package org.streets.database.datadict.impl;

import java.util.ArrayList;
import java.util.List;

import org.streets.commons.util.StringUtils;
import org.streets.database.SQLNestedException;
import org.streets.database.datadict.Relation;
import org.streets.database.datadict.TableRelations;
/**
 * 
 * @author dzb
 *
 */
public class TableRelationsImpl implements TableRelations {
	
	private final List<Relation> relations = new ArrayList<Relation>();

	public TableRelationsImpl() {
		
	}
	
	public TableRelations add(String express) {
		String host = StringUtils.left(express, express.indexOf("->"));
		if (StringUtils.isBlank(host) || host.indexOf(".") <= 0) {
			throw new SQLNestedException("Invalid relation express, lost host.");
		}
		String target = StringUtils.right(express, express.indexOf(">"));
		if (StringUtils.isBlank(target) || target.indexOf(".") <= 0) {
			throw new SQLNestedException("Invalid relation express, lost target.");
		}
		relations.add(new RelationImpl(express));
		return this;
	}
	
	public List<Relation> getRelations() {
		return relations;
	}
}
